package com.ccys.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.app.service.CallStandByScreenService;
import com.ccys.common.vo.ResponseResultApp;
import com.ccys.db.enums.PrescriptionFrequencyEnum;
import com.ccys.db.mapper.TblBookingMapper;
import com.ccys.db.vo.app.callStandByScreen.CheckConsultReadyVo;
import com.ccys.db.vo.app.callStandByScreen.PrescriptionMakeVo;
import com.ccys.db.vo.app.callStandByScreen.StepDoseMedicationsMakeVo;
import com.ccys.db.vo.app.splashscreen.AllergiesMakeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: WhiteCoat
 * @description: 呼叫时待机页面的接口实现类
 * @author: ZhangJingCan
 * @create: 2024-01-23 14:00
 **/

@Service
public class CallStandByScreenServiceImpl implements CallStandByScreenService {

    @Resource
    private TblBookingMapper tblBookingMapper;

    @Override
    public ResponseResultApp<CheckConsultReadyVo> checkConsultReady(String bookingId) {
        CheckConsultReadyVo vo = tblBookingMapper.appFindById(bookingId);
        //不为空才进
        if (ObjectUtil.isNotEmpty(vo)) {
            //取出处方集合 - 注意，这里的处方包含后续数据 - 根据index区分
            if (ObjectUtil.isNotEmpty(vo.getPrescription())) {
                //根据药品ID分组 - 并且根据indexOrder升序排序
                Map<String, List<PrescriptionMakeVo>> byProductId = vo.getPrescription()
                        .stream()
                        .collect(Collectors.groupingBy(PrescriptionMakeVo::getId,
                                // 使用下游收集器对每个分组进行排序
                                Collectors.collectingAndThen(
                                        // 使用 toList() 收集器将分组的元素收集到列表中
                                        Collectors.toList(),
                                        prescriptionList ->
                                                // 对每个分组的列表按照 indexOrder 属性升序排序
                                                prescriptionList.stream()
                                                        .sorted(Comparator.comparingInt(PrescriptionMakeVo::getIndexOrder))
                                                        .collect(Collectors.toList())
                                )
                        ));
                //处方集合
                List<PrescriptionMakeVo> prescriptionMakeVoList = new ArrayList<>();
                //使用map，然后循环去组装数据
                for (String key : byProductId.keySet()) {
                    PrescriptionMakeVo prescriptionMakeVo = new PrescriptionMakeVo();
                    List<StepDoseMedicationsMakeVo> stepDoseMedicationsMakeVos = new ArrayList<>();
                    //从map取出集合 - 注意,这里的集合已经升序排序过,所以第一个是最小的
                    for (PrescriptionMakeVo item : byProductId.get(key)) {
                        //设置频率说明 - 从枚举中获取
                        if (ObjectUtil.isNotEmpty(item.getFrequency())) {
                            item.setFrequencyDescription(PrescriptionFrequencyEnum.valueOf(item.getFrequency()).getValue());
                        }

                        //判断是否存在THEN 或者AND - 存在则代表当前循环为下一层数据 - 需要组装数据用于循环完成之后set进数据
                        if (ObjectUtil.isEmpty(item.getStepDose())) {
                            prescriptionMakeVo = item;
                        } else {
                            //准备下级数据
                            StepDoseMedicationsMakeVo stepDoseMedicationsMakeVo = new StepDoseMedicationsMakeVo();
                            //同名复制
                            BeanUtils.copyProperties(item, stepDoseMedicationsMakeVo);
                            //装入集合 - 循环结束了再set进PrescriptionMakeVo里面
                            stepDoseMedicationsMakeVos.add(stepDoseMedicationsMakeVo);
                        }
                    }
                    //将组装好的数据放入
                    prescriptionMakeVo.setStepDoseMedications(stepDoseMedicationsMakeVos);
                    //处方装入集合
                    prescriptionMakeVoList.add(prescriptionMakeVo);
                }
                //处方集合装入Vo
                vo.setPrescription(prescriptionMakeVoList);
            }

            //过敏症状集合
            Integer index = 1;
            for (AllergiesMakeVo allergy : vo.getAllergies()) {
                allergy.setIndex(index);
                index++;
            }
        }

        return ResponseResultApp.success(vo);
    }

}
